package com.group.eams.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group.eams.mapper.AssetClaimMapper;
import com.group.eams.mapper.FixedAssetAllMapper;
import com.group.eams.pojo.dto.*;
import com.group.eams.pojo.entity.AssetClaim;
import com.group.eams.pojo.entity.AssetClaim;
import com.group.eams.pojo.vo.AssetClaimListVo;
import com.group.eams.pojo.vo.ClaimFixedDetailListVo;
import com.group.eams.pojo.vo.ClaimFixedListVo;
import com.group.eams.pojo.vo.FixedAssetListVo;
import com.group.eams.service.AssetClaimService;
import com.group.eams.service.FixedAssetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 恩哲欣
* @description 针对表【asset_claim(资产申领表)】的数据库操作Service实现
* @createDate 2024-10-29 16:09:34
*/
@Service
public class AssetClaimServiceImpl extends ServiceImpl<AssetClaimMapper, AssetClaim> implements AssetClaimService{

    @Autowired
    private FixedAssetService fixedAssetService;
    @Autowired
    private FixedAssetAllMapper fixedAssetAllMapper;

    @Override
    public Page<AssetClaimListVo> findAssetClaimByWhere(AssetClaimQueryDto assetClaimQueryDto) {
        Page<AssetClaimListVo> claimListVoPage = new Page<>(
                assetClaimQueryDto.getPage(),
                assetClaimQueryDto.getLimit());
        return getBaseMapper().findAssetClaimByWhere(claimListVoPage,assetClaimQueryDto);
    }

    @Override
    public Page<ClaimFixedListVo> findClaimFixedByWhere(FixedAssetQueryDto fixedAssetQueryDto) {
        Page<FixedAssetListVo> fixedAssetListVoPage = fixedAssetService.findByQueryDto(fixedAssetQueryDto);
        List<ClaimFixedListVo> claimFixedListVoList = fixedAssetListVoPage.getRecords().stream()
                .map(fixedAssetListVo ->
                        new ClaimFixedListVo(
                                fixedAssetListVo.getId(),
                                fixedAssetListVo.getNumber(),
                                fixedAssetListVo.getAssetName(),
                                fixedAssetListVo.getModel(),
                                fixedAssetListVo.getAssetCategoryName(),
                                fixedAssetListVo.getUnit(),
                                fixedAssetListVo.getNowStorageLocationName(),
                                fixedAssetListVo.getAssetStatus()
                        ))
                .filter(item-> item.getAssetStatus() == 2)
                .collect(Collectors.toList());
        // 修正这里，添加 records 参数
        Page<ClaimFixedListVo> claimFixedListVoPage = new Page<>();
        claimFixedListVoPage.setTotal(claimFixedListVoList.size());
        claimFixedListVoPage.setRecords(claimFixedListVoList);
        return claimFixedListVoPage;
    }

    @Override
    public Page<ClaimFixedDetailListVo> addClaimFixedDetail(AssetPartDto assetPartDto) {
        List<FixedAssetListVo> fixedAssetList = fixedAssetAllMapper.fixedAssetAllListVo();
        List<FixedAssetListVo> fixedAssetListVos = fixedAssetList.stream()
                .filter(fixedAssetListVo -> assetPartDto.getAssetIds().contains(fixedAssetListVo.getId()))
                .collect(Collectors.toList());
        List<ClaimFixedDetailListVo> claimFixedDetailListVos = fixedAssetListVos.stream().map(fixedAssetListVo ->
                new ClaimFixedDetailListVo(
                        fixedAssetListVo.getId(),
                        fixedAssetListVo.getAssetName(),
                        fixedAssetListVo.getNumber(),
                        fixedAssetListVo.getModel(),
                        fixedAssetListVo.getAssetCategoryName(),
                        fixedAssetListVo.getUnit(),
                        fixedAssetListVo.getAdminName(),
                        fixedAssetListVo.getUserName(),
                        fixedAssetListVo.getNowStorageLocationName(),
                        fixedAssetListVo.getTargetStorageLocationName()
                )).collect(Collectors.toList());
        Page<ClaimFixedDetailListVo> claimFixedDetailListVoPage = new Page<>();
        claimFixedDetailListVoPage.setTotal(claimFixedDetailListVos.size());
        claimFixedDetailListVoPage.setRecords(claimFixedDetailListVos);
        return claimFixedDetailListVoPage;
    }

    @Override
    public Page<ClaimFixedDetailListVo> findClaimFixedDetail(ApplyNumFixedAssetQueryDto applyNumFixedAssetQueryDto) {
        List<Long> assetIds = getBaseMapper().getAssetIds(applyNumFixedAssetQueryDto.getApplicationNum());
        List<FixedAssetListVo> fixedAssetList = fixedAssetAllMapper.fixedAssetAllListVo();
        List<FixedAssetListVo> fixedAssetListVos = fixedAssetList.stream()
                .filter(fixedAssetListVo -> assetIds.contains(fixedAssetListVo.getId()))
                .collect(Collectors.toList());
        List<ClaimFixedDetailListVo> claimFixedDetailListVos = fixedAssetListVos.stream().map(fixedAssetListVo ->
                new ClaimFixedDetailListVo(
                        fixedAssetListVo.getId(),
                        fixedAssetListVo.getAssetName(),
                        fixedAssetListVo.getNumber(),
                        fixedAssetListVo.getModel(),
                        fixedAssetListVo.getAssetCategoryName(),
                        fixedAssetListVo.getUnit(),
                        fixedAssetListVo.getAdminName(),
                        fixedAssetListVo.getUserName(),
                        fixedAssetListVo.getNowStorageLocationName(),
                        fixedAssetListVo.getTargetStorageLocationName()
                )).collect(Collectors.toList());
        Page<ClaimFixedDetailListVo> claimFixedDetailListVoPage = new Page<>();
        claimFixedDetailListVoPage.setTotal(claimFixedDetailListVos.size());
        claimFixedDetailListVoPage.setRecords(claimFixedDetailListVos);
        return claimFixedDetailListVoPage;
    }

    @Override
    public int deleteByApplicationNum(BatchDeleteIdsDto batchDeleteIdsDto) {
        // 检查 applicationNum 和 assetIds 是否都不为空，只有都不为空时才进行删除
        if (StrUtil.isEmpty(batchDeleteIdsDto.getApplicationNum()) || CollUtil.isEmpty(batchDeleteIdsDto.getAssetIds())) {
            return 0;  // 如果满足条件之一为空，则不执行删除，返回0
        }

        LambdaQueryWrapper<AssetClaim> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        // 加入删除条件：applicationNum 非空
        lambdaQueryWrapper.eq(AssetClaim::getApplicationNum, batchDeleteIdsDto.getApplicationNum());

        // 加入删除条件：assetIds 非空
        lambdaQueryWrapper.in(AssetClaim::getAssetId, batchDeleteIdsDto.getAssetIds());

        // 执行删除并返回删除的记录数
        return getBaseMapper().delete(lambdaQueryWrapper);
    }

    @Override
    public int updateByApplicationNum(BatchUpdateIdsDto<AssetClaim> batchUpdateIdsDto) {
        // 检查 applicationNum 和 assetIds 是否都不为空，只有都不为空时才进行修改
        if (StrUtil.isEmpty(batchUpdateIdsDto.getApplicationNum()) ||
                CollUtil.isEmpty(batchUpdateIdsDto.getAssetIds()) ||
                ObjectUtil.isEmpty(batchUpdateIdsDto.getT())) {
            return 0;  // 如果满足条件之一为空，则不执行修改，返回0
        }

        LambdaUpdateWrapper<AssetClaim> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();

        // 设置查询条件
        lambdaUpdateWrapper.eq(AssetClaim::getApplicationNum, batchUpdateIdsDto.getApplicationNum());
        lambdaUpdateWrapper.in(AssetClaim::getAssetId, batchUpdateIdsDto.getAssetIds());

        // 获取要更新的实体对象
        AssetClaim assetClaim = batchUpdateIdsDto.getT();

        // 批量更新字段的方法，避免重复代码
        updateField(lambdaUpdateWrapper, assetClaim.getState(), AssetClaim::getState);
        updateField(lambdaUpdateWrapper, assetClaim.getTitle(), AssetClaim::getTitle);
        updateField(lambdaUpdateWrapper, batchUpdateIdsDto.getApplicationNum(), AssetClaim::getApplicationNum);
        updateField(lambdaUpdateWrapper, assetClaim.getApplicationDate(), AssetClaim::getApplicationDate);
        updateField(lambdaUpdateWrapper, assetClaim.getApplicantId(), AssetClaim::getApplicantId);
        updateField(lambdaUpdateWrapper, assetClaim.getCollectionDate(), AssetClaim::getCollectionDate);
        updateField(lambdaUpdateWrapper, assetClaim.getApplicationReason(), AssetClaim::getApplicationReason);
        updateField(lambdaUpdateWrapper, assetClaim.getRemark(), AssetClaim::getRemark);
        updateField(lambdaUpdateWrapper, assetClaim.getFlowId(), AssetClaim::getFlowId);
        updateField(lambdaUpdateWrapper, assetClaim.getCurrentNode(), AssetClaim::getCurrentNode);

        // 执行修改并返回修改的记录数
        return getBaseMapper().update(null, lambdaUpdateWrapper);
    }

    @Override
    public AssetClaim findClaimByNum(String applicationNum) {
        if (StrUtil.isEmpty(applicationNum)) {
            return null;
        }
        return getBaseMapper().findClaimByNum(applicationNum);
    }

    // 使用 SFunction 进行字段更新
    private <T> void updateField(LambdaUpdateWrapper<AssetClaim> wrapper, T value, SFunction<AssetClaim, T> column) {
        if (value != null) {
            wrapper.set(column, value);
        }
    }

    private <T> void updateField(LambdaUpdateWrapper<AssetClaim> wrapper, T value, T defaultValue, SFunction<AssetClaim, T> column) {
        if (value != null && !value.equals(defaultValue)) {
            wrapper.set(column, value);
        }
    }
}
